package com.molyfun.parents.activity.setting;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.molyfun.parents.MFPApplication;
import com.molyfun.parents.R;
import com.molyfun.parents.activity.ask.view.TopicOperationView;
import com.molyfun.parents.activity.common.BaseActivity;
import com.molyfun.parents.rest.BaseResponse;
import com.molyfun.parents.utils.Const;
import com.molyfun.parents.view.common.MfEditText;

import butterknife.ButterKnife;
import butterknife.InjectView;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/**
 * Created by william(liuchao.hit@gmail.com).
 * Date:11/23/16.
 */
public class SuggestionActivity extends BaseActivity implements View.OnClickListener {

    @InjectView(R.id.base_title_left)
    ImageView mBack;
    @InjectView(R.id.suggestion_edt)
    MfEditText suggestionEdt;
    @InjectView(R.id.submit)
    TextView mSubmit;
    @InjectView(R.id.base_title)
    TextView mTitle;

    private String mTopicId;

    private int mCurrentOperationType = TopicOperationView.OperationType.TYPE_TOPIC;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_suggestion);
        ButterKnife.inject(this);

        getSupportActionBar().setTitle("");
        mTitle.setText("意见反馈");

        Intent intent = getIntent();
        mCurrentOperationType = intent.getIntExtra("type", TopicOperationView.OperationType.TYPE_TOPIC);

        mBack.setOnClickListener(this);
        mSubmit.setOnClickListener(this);
        if (getIntent() != null) {
            mTopicId = getIntent().getStringExtra(Const.TOPIC_ID);
        }

    }

    public static void startActivity(Context context) {
        Intent intent = new Intent();
        intent.setClass(context, SuggestionActivity.class);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String topicId) {
        Intent intent = new Intent();
        intent.setClass(context, SuggestionActivity.class);
        intent.putExtra(Const.TOPIC_ID, topicId);
        context.startActivity(intent);
    }

    public static void startActivity(Context context, String topicId, int type) {
        Intent intent = new Intent();
        intent.setClass(context, SuggestionActivity.class);
        intent.putExtra(Const.TOPIC_ID, topicId);
        intent.putExtra("type", type);
        context.startActivity(intent);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.base_title_left:
                finish();
                break;
            case R.id.submit:
                if(!TextUtils.isEmpty(suggestionEdt.getText().toString().trim())){
                    sendSuggestion(suggestionEdt.getText().toString().trim());
                }else{
                    toast("请输入内容");
                }
                break;
            default:
                break;
        }
    }

    private void sendSuggestion(String suggestion) {
        showProgressDialog();

        if (TextUtils.isEmpty(mTopicId)) {
            Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().sendSuggestion(suggestion);
            call.enqueue(new Callback<BaseResponse>() {
                @Override
                public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                    dismissProgressDialog();
                    if (response.body() != null) {
                        if (response.body().getCode() == 200) {
                            toast("提交成功");
                            finish();
                        } else {
                            toast(response.body().getMsg());
                        }
                    }
                }

                @Override
                public void onFailure(Call<BaseResponse> call, Throwable t) {
                    dismissProgressDialog();
                    toast("提交失败");
                }
            });

        } else {
            if(mCurrentOperationType == TopicOperationView.OperationType.TYPE_TOPIC){
                Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().addHomelandSuggestion(suggestion, mTopicId);
                call.enqueue(new Callback<BaseResponse>() {
                    @Override
                    public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                        dismissProgressDialog();
                        if (response.body() != null) {
                            if (response.body().getCode() == 200) {
                                toast("提交成功");
                                finish();
                            } else {
                                toast(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResponse> call, Throwable t) {
                        dismissProgressDialog();
                        toast("提交失败");
                    }
                });
            }else{
                Call<BaseResponse> call = MFPApplication.getInstance().getRestClient().addTopicSuggestion(suggestion, mTopicId);
                call.enqueue(new Callback<BaseResponse>() {
                    @Override
                    public void onResponse(Call<BaseResponse> call, Response<BaseResponse> response) {
                        dismissProgressDialog();
                        if (response.body() != null) {
                            if (response.body().getCode() == 200) {
                                toast("提交成功");
                                finish();
                            } else {
                                toast(response.body().getMsg());
                            }
                        }
                    }

                    @Override
                    public void onFailure(Call<BaseResponse> call, Throwable t) {
                        dismissProgressDialog();
                        toast("提交失败");
                    }
                });
            }
        }


    }

}
